<?php
class OrderController extends Controller
{
    function actionIndex()
    {
        $order = new OrderModel();
        $cities = $order->getAllCities();
        $products = $order->getAllProducts();
        $this->view->render('order', array(
            'cities' => $cities,
            'products' => $products
        ));
    }

    function actionCreate()
    {
        require_once __DIR__ . '/../models/UserModel.php';
        $post = $_POST;
        $user = new UserModel();
        $user->attributes['name'] = $post['name'];
        $user->attributes['email'] = $post['email'];
        $user->attributes['phone'] = $post['phone'];
        $user->attributes['city_id'] = $post['city_id'];
        $user->attributes['status'] = UserModel::STATUS_ACTIVE;
        $user->attributes['role'] = UserModel::ROLE_USER;
        if ($user->insert()){
            $newUser = $user->getUserByEmail($user->attributes['email']);
            $order = new OrderModel();
            $order->attributes['user_id'] = $newUser->id;
            $order->attributes['product_id'] = $post['product_id'];
            $order->attributes['quantity'] = $post['quantity'];
            $order->attributes['required_date'] = date('Y-m-d H:i:s', (time()+(3600*24*3)));
            if ($order->insert())
                return json_encode(['result' => true]);
        }
        return json_encode(['result' => false]);
    }

}